package com.learn.service.second.api.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.learn.service.second.api.ErEmpMapper;
import com.learn.service.second.api.base.BaseApiService;
import com.learn.service.second.api.base.BaseResponse;
import com.learn.service.second.pojo.ErEmpEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 员工登录
 */
@RestController
@CrossOrigin
@Api("用户登陆接口")
public class LoginServiceImpl extends BaseApiService<JSONObject> {

    @Autowired
    ErEmpMapper erEmpMapper;

    // 手机号userEntityList
    List<ErEmpEntity> userEntityList;

    /**
     * 缓存所有手机号，前端每点击3次请求一回
     * @return
     */
    @PostMapping("/allPhone")
    public List<ErEmpEntity> allPhone(){
        userEntityList= erEmpMapper.selectList(null);
        System.out.println("11");
        return userEntityList;
    }

    /**
     * 获得手机号验证是否存在，存在可以获取验证码
     * @param request
     * @return
     */
    @PostMapping("/phoneShow")
    @ApiOperation(value = "登录验证手机号", notes = "用户登陆")
    @ApiImplicitParams({@ApiImplicitParam(name = "phone", value = "手机号码", required = true,
            dataType = "String")})
    public BaseResponse<JSONObject> phoneShow(HttpServletRequest request){
        boolean kep=false;
        String phone = request.getParameter("phone");//手机号
        for (ErEmpEntity e:userEntityList) {
            if (e.getEPhone().equals(phone)){
                kep = true;
                break;
            }else {
                kep=false;
            }
        }
        return kep?setResultSuccess("手机号正确可以获得验证码登录"):setResultError("手机号错误");
    }

    /**
     * 登陆接口,账号和密码
     * @param erEmpEntity
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "登陆接口", notes = "用户登陆")
    @ApiImplicitParams({@ApiImplicitParam(name = "eAccount", value = "账号", required = true,
            dataType = "String"), @ApiImplicitParam(name = "ePassword", value = "密码", required = true,
            dataType = "String")})
    public BaseResponse<JSONObject> login(ErEmpEntity erEmpEntity) {
        QueryWrapper<ErEmpEntity> queryWrapper = new QueryWrapper<ErEmpEntity>();
        //账号验证
        queryWrapper.eq(StringUtils.isNoneEmpty(erEmpEntity.getEAccount()),"e_account",erEmpEntity.getEAccount());
        //密码验证
        queryWrapper.eq(StringUtils.isNoneEmpty(erEmpEntity.getEPassword()),"e_password",erEmpEntity.getEPassword());

        return erEmpMapper.selectOne(queryWrapper)!=null?setResultSuccess("登陆成功"):setResultError("登陆失败");
    }
}
